package edu.neepu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class YainClass {

//    @Test
//    public void test1() throws Exception {
//        System.out.println("老师！！！！苏宇轩没跟上！！！");
//        Document document = Jsoup.connect("http://www.baidu.com").get();
//
//        System.out.println(document);
//    }
//
//    @Test
//    public void test2() throws Exception {
//        String html = "<html><head><title>网页标题</title></head><body><p>一段文字</p></body></html>";
//
//        Document document = Jsoup.parse(html);
//        System.out.println(document);
//    }
//
//    @Test
//    public void test3() throws Exception {
//        File file = new File("C:\\Users\\ljrzz\\IdeaProjects\\YYYYYY\\src\\main\\resources\\main.html");
//        Document document = Jsoup.parse(file, "utf-8");
//
//        Elements elements = document.getElementsByTag("p");
////        System.out.println(elements);
//
////        for (Element element : elements) {
////            System.out.println(elements.text());
////        }
//
//        //按id
////        Element p1 = document.getElementById("p1");
////        //按class
////        Elements c3 = document.getElementsByClass("c3");
//
//
//        System.out.println(document.select("#p1"));
//        System.out.println(document.select(".c3"));
//        System.out.println(document.select("p"));
//    }
//
//    @Test
//    public void test4() throws Exception {
//        Document document = Jsoup.connect("http://www.csdn.net/").get();
//        Elements elements = document.select("a[href]");
//        Elements elements1 = document.select("img[src]");
////        System.out.println(elements);
//
////        for (Element element : elements) {
////            System.out.println(element.attr("abs:href"));
////        }
//        for (Element element : elements1) {
//            System.out.println(element.attr("abs:src"));
//        }
//    }
//
//    @Test
//    public void test5() throws Exception {
//        //知乎爬取
//        Document document = Jsoup.connect("https://www.zhihu.com/explore").get();
//        Elements elements = document.select(".summary");
//
//
//        for (Element element : elements) {
//            System.out.println(element.text());
//            System.out.println(" ");
//
//        }
//    }
//
//    @Test
//    public void test6() throws Exception {
//
//        Document document = Jsoup.connect("https://www.zhihu.com/explore").header("Accept.language","zh-cn").get();
//        Elements elements = document.select(".feed-item");
//
//        for (Element element : elements) {
//            Element e = element.select(".question_link").first();
//            if (e != null) {
//                System.out.println("问题标题" + e.text());
//                System.out.println("问题链接" + e.select("abs:href"));
//            }
//
//        }
//    }
//    @Test
//    public void test7() throws Exception{
//        for(int i = 1;i<10;i++) {
//            Document document =
//                    Jsoup.connect("https://=store.steampowered.com/search/?specials=" + i).header("Accept-language","zh-cn").get();
//            Elements elements = document.select(".search_result_row");
//
//            for (Element element : elements) {
//                Elements e1 = element.select(".title");
//                System.out.println("游戏名称:" + e1.text());
//                Elements released = element.select(".search_released");
//                System.out.println("日期 :" + released.text());
//            }
//        }
//    }
    @Test
    public void test8() throws Exception{
        for(int i = 1;i<11;i++) {//翻页
            Document document = Jsoup.connect("https://bing.ioliu.cn/?p=" + i).get();//连接图片网
            Elements elements = document.select(".item");//选择ITEM类
            int len = -1;
                for (Element element : elements) {
                    Elements item = element.select(".description");//读取图片名
                    Elements imgsrc = element.select(".download");//读取下载地址
                    System.out.println(item.text());
                    System.out.println(imgsrc.attr("abs:href"));
                    String s = imgsrc.attr("abs:href");//抓取下载地址
                    String s1 = s.substring(s.lastIndexOf("/"),s.lastIndexOf("?"))+".jpg";//地址格式化
                    URL url = new URL(s);
                    URLConnection uri = url.openConnection();//打开下载地址
                    InputStream is = uri.getInputStream();//输入流
                    FileOutputStream os = new FileOutputStream(new File("f:\\img"+s1));//输出流
                    while((len = is.read()) != -1){
                        os.write(len);//写入
                    }
                }
        }
    }
    //12.27
    @Test
    public void test9() throws Exception{
        Document document = Jsoup.connect("http://www.shicimingju.com/book/").get();
        Elements elements = document.select(".bookmark-list");
        Elements shuming = elements.select("a");
        for (Element element : shuming) {
            String url1 = element.attr("abs:href");
            String bookname = element.select("a").text();
            File dir = new File("f:\\小说" + "\\" + bookname);
            if(!dir.exists()){
                dir.mkdirs();
            }
            System.out.println(url1);
            Document dd = Jsoup.connect(url1).get();
            Elements bookcontain = dd.select(".book-mulu");
            Elements ed = bookcontain.select("li");
            for (Element element1 : ed) {
                Elements dclass = element1.select("a");
                Document doc1 = Jsoup.connect("http://www.shicimingju.com" + dclass.attr("href")).get();
                Elements title = doc1.select(".www-main-container").select("h1");
                Elements art = doc1.select(".chapter_content");
                Elements pay = art.select("p");
                String text = pay.text();
                System.out.println(title.text());
                byte[] bytes = text.getBytes();
                File file = new File(dir + "\\" + title.text() + ".txt");
                if(!file.exists()){
                    file.createNewFile();
                }
                FileOutputStream os = new FileOutputStream(file);
                os.write(bytes);
                System.out.println("打印完成");
            }
        }
    }

    }


